﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition.UserCenter
{
    public partial class Index : BaseUserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Title = "用户中心--"+WebTitle;
            if (!IsPostBack)
            {
                LoadUser();
                GetUCode();
            }
        }

        private void GetUCode()
        {
            //获取区域
            List<AreaModel> areas = CenterService.areaService.GetListByPid(0);
            areaPid.DataSource = areas;
            areaPid.DataTextField = "AreaName";
            areaPid.DataValueField = "Id";
            areaPid.DataBind();

            StringBuilder sb = new StringBuilder();
            List<CodesModel> cdms = CenterService.codesService.GetModels(2); 
            foreach (var c in cdms) {
                string strchk = string.Empty;
                if (LoginUser != null) {
                    if (!string.IsNullOrEmpty(LoginUser.ContactType))
                    {
                        if (LoginUser.ContactType.Contains(c.ECode))
                        {
                            strchk = " checked='checked' ";
                        }
                    }
                }

                sb.AppendFormat("<input type='checkbox' name='chkct' value='{0}' {2}/>请{1}联系我&nbsp;",c.ECode,c.EName,strchk);
            }
            ltrContact.Text = sb.ToString();

        }

        private void LoadUser()
        {
            if (LoginUser != null) {
                ltrUserName.Text = LoginUser.UserName;
                RealName.Value = LoginUser.RealName;
                ltrEmail.Text = LoginUser.Email;
                ltrPhone.Text = LoginUser.Mobile;
                CompName.Value = LoginUser.CompName;
                Position.Value = LoginUser.Position;
                
                if (LoginUser.compMdl != null)
                {
                    hfId.Value = LoginUser.compMdl.Id.ToString();
                    PostCode.Value = LoginUser.compMdl.PostCode;
                    ContactMan.Value = LoginUser.compMdl.Contact;
                    PhoneStart.Value = LoginUser.compMdl.PhoneStart;
                    PhoneNum.Value = LoginUser.compMdl.PhoneNum;
                    FaxStart.Value = LoginUser.compMdl.FaxStart;
                    FaxNum.Value = LoginUser.compMdl.FaxNum;
                    CompWebSite.Value = LoginUser.compMdl.CompSite;
                }
                else {
                    hfId.Value = "0";
                }
            
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (LoginUser != null) {
                LoginUser.UserAddress = Address.Value;
                LoginUser.CompName = CompName.Value;
                UserCompModel cpmdl = new UserCompModel();
                cpmdl.CompName = CompName.Value;
                cpmdl.Contact = ContactMan.Value;
                cpmdl.CompSite = CompWebSite.Value;
                cpmdl.FaxStart = FaxStart.Value;
                cpmdl.FaxNum = FaxNum.Value;
                cpmdl.PhoneStart = PhoneStart.Value;
                cpmdl.PhoneNum = PhoneNum.Value;
                cpmdl.UserId = LoginUser.Id;
                cpmdl.PostCode = PostCode.Value;
                cpmdl.CountryCode = "086";
                cpmdl.IsDelete = 0;
                cpmdl.CompLevel = 0;
                cpmdl.CompAddr = Address.Value;
                if (!string.IsNullOrEmpty(cpmdl.CompLogo)) {
                    spPic.InnerHtml =string.Format( "<img src='{0}' width='100' height='100'/>",cpmdl.CompLogo);
                }

                //网站ID 用户网站id
                cpmdl.SiteID =LoginUser.SiteID;
                LoginUser.Position = Position.Value;
                LoginUser.ContactType = Request["chkct"];
                LoginUser.compMdl = cpmdl;
                if (CenterService.userService.Save(LoginUser) > 0)
                {
                    JsHelper.Alert(this, CommFunMsg.GetMessage("a0001", Lan.zh));
                    return;
                }
                else {
                    JsHelper.Alert(this, CommFunMsg.GetMessage("e0001", Lan.zh));
                }

                //cpmdl.CompLogo = upLogo.PostedFile;

            }
        }
    }
}